<!--
 * @Description: 
 * @Author: mxy
 * @Date: 2022-11-11 21:41:41
 * @LastEditors: mxy
 * @LastEditTime: 2022-11-11 21:48:06
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button {
            width: 100px;
            height: 30px;
            background-color: skyblue;
            color: white;
            border: 1px solid pink;
        }
    </style>
</head>

<body>
    <!-- 因为上传文件框不好看且不能设置样式，所以一般隐藏他 -->
    <input type="file" style="display: none;" multiple>
    <button>选择文件</button>
    <img src="" alt="">
    <script src="./axios.js"></script>
    <script>
        document.querySelector('button').addEventListener('click', function (e) {
            e.preventDefault()
            document.querySelector('input').click()
        })
        document.querySelector('input').addEventListener('change', function () {
            if (this.files.length > 0) {
                let fd = new FormData()
                let fileObj = this.files[0]
                fd.append('avatar', fileObj)
                axios.post('http://www.itcbc.com:3006/api/formdata', fd).then(({ data: res }) => {
                    document.querySelector('img').src = res.data.filename
                })
            }
        })
    </script>
</body>

</html>